Node.js MongoDB Find

Node.js ஐப் பயன்படுத்தி MongoDB தொகுப்புகளில் தரவைக் கண்டறிய கற்றுக்கொள்ளுங்கள்

அறிமுகம்

MongoDB இல் ஒரு தொகுப்பில் தரவைக் கண்டறிய find மற்றும் findOne முறைகளைப் பயன்படுத்துகிறோம்.

MySQL தரவுத்தளத்தில் ஒரு அட்டவணையில் தரவைக் கண்டறிய SELECT அறிக்கை பயன்படுத்தப்படுவது போலவே.

findOne()

முதல் பொருத்தத்தை மட்டும் திருப்பித் தரும்

  • ஒற்றை ஆவணம்
  • விரைவான கண்டறிதல்
  • குறிப்பிட்ட பதிவு

find()

அனைத்து பொருத்தங்களையும் திருப்பித் தரும்

  • பல ஆவணங்கள்
  • முழு தொகுப்பு
  • விரிவான முடிவுகள்

ஒன்றைக் கண்டறிதல்

MongoDB இல் ஒரு தொகுப்பிலிருந்து தரவைத் தேர்ந்தெடுக்க, நாம் findOne() முறையைப் பயன்படுத்தலாம்.

findOne() முறை தேர்வில் முதல் நிகழ்வைத் திருப்பித் தரும்.

findOne() முறையின் முதல் அளவுரு ஒரு வினா பொருளாகும். இந்த எடுத்துக்காட்டில் நாம் ஒரு வெற்று வினா பொருளைப் பயன்படுத்துகிறோம், இது ஒரு தொகுப்பில் உள்ள அனைத்து ஆவணங்களையும் தேர்ந்தெடுக்கும் (ஆனால் முதல் ஆவணத்தை மட்டுமே திருப்பித் தரும்).

எடுத்துக்காட்டு

customers தொகுப்பில் முதல் ஆவணத்தைக் கண்டறியவும்:

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result.name);
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_findone.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_findone.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

Company Inc.

அனைத்தையும் கண்டறிதல்

MongoDB இல் ஒரு அட்டவணையிலிருந்து தரவைத் தேர்ந்தெடுக்க, நாம் find() முறையையும் பயன்படுத்தலாம்.

find() முறை தேர்வில் உள்ள அனைத்து நிகழ்வுகளையும் திருப்பித் தரும்.

find() முறையின் முதல் அளவுரு ஒரு வினா பொருளாகும். இந்த எடுத்துக்காட்டில் நாம் ஒரு வெற்று வினா பொருளைப் பயன்படுத்துகிறோம், இது தொகுப்பில் உள்ள அனைத்து ஆவணங்களையும் தேர்ந்தெடுக்கும்.

find() முறையில் எந்த அளவுருக்களும் இல்லை என்பது MySQL இல் SELECT * போன்றதே.

எடுத்துக்காட்டு

customers தொகுப்பில் உள்ள அனைத்து ஆவணங்களையும் கண்டறியவும்:

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_find.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_find.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

[
  { _id: 58fdbf5c0ef8a50b4cdd9a84 , name: 'John', address: 'Highway 71'},
  { _id: 58fdbf5c0ef8a50b4cdd9a85 , name: 'Peter', address: 'Lowstreet 4'},
  { _id: 58fdbf5c0ef8a50b4cdd9a86 , name: 'Amy', address: 'Apple st 652'},
  { _id: 58fdbf5c0ef8a50b4cdd9a87 , name: 'Hannah', address: 'Mountain 21'},
  { _id: 58fdbf5c0ef8a50b4cdd9a88 , name: 'Michael', address: 'Valley 345'},
  { _id: 58fdbf5c0ef8a50b4cdd9a89 , name: 'Sandy', address: 'Ocean blvd 2'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8a , name: 'Betty', address: 'Green Grass 1'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8b , name: 'Richard', address: 'Sky st 331'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8c , name: 'Susan', address: 'One way 98'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8d , name: 'Vicky', address: 'Yellow Garden 2'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8e , name: 'Ben', address: 'Park Lane 38'},
  { _id: 58fdbf5c0ef8a50b4cdd9a8f , name: 'William', address: 'Central st 954'},
  { _id: 58fdbf5c0ef8a50b4cdd9a90 , name: 'Chuck', address: 'Main Road 989'},
  { _id: 58fdbf5c0ef8a50b4cdd9a91 , name: 'Viola', address: 'Sideway 1633'}
]

சிலவற்றைக் கண்டறிதல்

find() முறையின் இரண்டாவது அளவுரு என்பது முடிவில் சேர்க்க வேண்டிய புலங்களை விவரிக்கும் projection பொருளாகும்.

இந்த அளவுரு விருப்பமானது, மற்றும் தவிர்க்கப்பட்டால், அனைத்து புலங்களும் முடிவில் சேர்க்கப்படும்.

எடுத்துக்காட்டு

customers தொகுப்பில் உள்ள அனைத்து ஆவணங்களின் "name" மற்றும் "address" புலங்களைத் திருப்பி அனுப்பவும்:

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}, { projection: { _id: 0, name: 1, address: 1 } }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

மேலே உள்ள குறியீட்டை "demo_mongodb_find_fields.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:

C:\Users\Your Name>node demo_mongodb_find_fields.js

இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:

[
  { name: 'John', address: 'Highway 71'},
  { name: 'Peter', address: 'Lowstreet 4'},
  { name: 'Amy', address: 'Apple st 652'},
  { name: 'Hannah', address: 'Mountain 21'},
  { name: 'Michael', address: 'Valley 345'},
  { name: 'Sandy', address: 'Ocean blvd 2'},
  { name: 'Betty', address: 'Green Grass 1'},
  { name: 'Richard', address: 'Sky st 331'},
  { name: 'Susan', address: 'One way 98'},
  { name: 'Vicky', address: 'Yellow Garden 2'},
  { name: 'Ben', address: 'Park Lane 38'},
  { name: 'William', address: 'Central st 954'},
  { name: 'Chuck', address: 'Main Road 989'},
  { name: 'Viola', address: 'Sideway 1633'}
]

⚠️ Projection விதிகள்:

  • ஒரே பொருளில் 0 மற்றும் 1 மதிப்புகளைக் குறிப்பிட அனுமதி இல்லை (_id புலம் தவிர)
  • 0 மதிப்புடன் ஒரு புலத்தைக் குறிப்பிட்டால், மற்ற அனைத்து புலங்களும் 1 மதிப்பைப் பெறும்
  • 1 மதிப்புடன் ஒரு புலத்தைக் குறிப்பிட்டால், மற்ற அனைத்து புலங்களும் 0 மதிப்பைப் பெறும்
  • _id புலத்தை விலக்க, அதன் மதிப்பை 0 ஆக அமைக்க வேண்டும்

கூடுதல் Projection எடுத்துக்காட்டுகள்

"address" புலத்தை விலக்குதல்

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}, { projection: { address: 0 } }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

மட்டும் "name" புலத்தைத் திருப்பி அனுப்புதல்

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}, { projection: { _id: 0, name: 1 } }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

_id புலத்தைத் தவிர்த்து அனைத்து புலங்களையும் திருப்பி அனுப்புதல்

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}, { projection: { _id: 0 } }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

பிழை எடுத்துக்காட்டு (0 மற்றும் 1 ஒன்றாக)

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  let dbo = db.db("mydb");
  dbo.collection("customers").find({}, { projection: { name: 1, address: 0 } }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

முடிவு பொருள்

மேலே உள்ள எடுத்துக்காட்டின் முடிவிலிருந்து நீங்கள் பார்க்க முடியும் என, முடிவை ஒவ்வொரு ஆவணத்தையும் ஒரு பொருளாகக் கொண்ட ஒரு வரிசையாக மாற்றலாம்.

எ.கா., மூன்றாவது ஆவணத்தின் முகவரியைத் திருப்பி அனுப்ப, மூன்றாவது வரிசை பொருளின் முகவரி பண்புக்கூறைக் குறிக்கவும்:

எடுத்துக்காட்டு

மூன்றாவது ஆவணத்தின் முகவரியைத் திருப்பி அனுப்பவும்:

console.log(result[2].address);

இது இந்த முடிவை உருவாக்கும்:

Apple st 652

நவீன Find எடுத்துக்காட்டுகள்

நவீன async/await தொடரியல்

const { MongoClient } = require('mongodb');

async function findDocuments() {
  const uri = "mongodb://localhost:27017/";
  const client = new MongoClient(uri);

  try {
    await client.connect();
    const database = client.db("mydb");
    const collection = database.collection("customers");
    
    // Find first document
    const firstDoc = await collection.findOne({});
    console.log("First document:", firstDoc);
    
    // Find all documents
    const allDocs = await collection.find({}).toArray();
    console.log("All documents:", allDocs.length);
    
    // Find with projection
    const projectedDocs = await collection.find({}, {
      projection: { _id: 0, name: 1, address: 1 }
    }).toArray();
    console.log("Projected documents:", projectedDocs);
    
  } catch (err) {
    console.error("Error finding documents:", err);
  } finally {
    await client.close();
  }
}

findDocuments();

Find சிறந்த நடைமுறைகள்

செயல்திறன்

  • தேவையான புலங்களை மட்டும் தேர்ந்தெடுக்கவும்
  • குறியீட்டு நெடுவரிசைகளில் வினா செய்யவும்
  • பெரிய முடிவு செட்களுக்கு LIMIT பயன்படுத்தவும்
  • தேவையற்ற தரவைத் தேர்ந்தெடுப்பதைத் தவிர்க்கவும்

குறியீடு தரம்

  • சரியான பிழை கையாளுதலையும் கண்காணிப்பையும் பயன்படுத்தவும்
  • நவீன async/await தொடரியலைப் பயன்படுத்தவும்
  • வாசிப்புக்காக வினாக்களை வடிவமைக்கவும்
  • மறுபயன்பாட்டிற்காக find செயல்பாடுகளை உருவாக்கவும்

தரவு அணுகல்

  • தேவைக்கேற்ப findOne() அல்லது find() பயன்படுத்தவும்
  • Projection மூலம் தரவுப் போக்குவரத்தைக் குறைக்கவும்
  • முடிவு அளவைக் கண்காணிக்கவும்
  • தரவு வடிவமைப்பைக் கருத்தில் கொள்ளவும்

முழுமையான எடுத்துக்காட்டு

அனைத்து find நுட்பங்களையும் உள்ளடக்கிய முழுமையான எடுத்துக்காட்டு:

const { MongoClient } = require('mongodb');

class DataFinder {
  constructor(connectionString, dbName) {
    this.connectionString = connectionString;
    this.dbName = dbName;
    this.client = new MongoClient(connectionString);
  }

  async connect() {
    try {
      await this.client.connect();
      console.log("Connected to MongoDB server");
      this.database = this.client.db(this.dbName);
      return true;
    } catch (error) {
      console.error("Connection failed:", error);
      return false;
    }
  }

  async findFirst(collectionName, query = {}) {
    try {
      const collection = this.database.collection(collectionName);
      const result = await collection.findOne(query);
      return result;
    } catch (error) {
      console.error("Error finding first document:", error);
      throw error;
    }
  }

  async findAll(collectionName, query = {}, projection = {}) {
    try {
      const collection = this.database.collection(collectionName);
      const cursor = collection.find(query, { projection });
      const result = await cursor.toArray();
      return result;
    } catch (error) {
      console.error("Error finding all documents:", error);
      throw error;
    }
  }

  async findWithLimit(collectionName, limit = 10, query = {}, projection = {}) {
    try {
      const collection = this.database.collection(collectionName);
      const cursor = collection.find(query, { projection }).limit(limit);
      const result = await cursor.toArray();
      return result;
    } catch (error) {
      console.error("Error finding with limit:", error);
      throw error;
    }
  }

  async close() {
    try {
      await this.client.close();
      console.log("Connection closed");
    } catch (error) {
      console.error("Error closing connection:", error);
    }
  }
}

// Usage example
async function main() {
  const connectionString = "mongodb://localhost:27017/";
  const dbName = "mydb";
  
  const finder = new DataFinder(connectionString, dbName);

  try {
    const connected = await finder.connect();
    if (!connected) return;

    // Find first document
    const firstCustomer = await finder.findFirst("customers");
    console.log("First customer:", firstCustomer);

    // Find all documents with projection
    const allCustomers = await finder.findAll("customers", {}, { 
      projection: { _id: 0, name: 1, address: 1 } 
    });
    console.log("All customers (projected):", allCustomers);

    // Find with limit
    const limitedCustomers = await finder.findWithLimit("customers", 5);
    console.log("First 5 customers:", limitedCustomers);

    console.log("All find operations completed successfully!");
    
  } catch (error) {
    console.error("Main function error:", error);
  } finally {
    await finder.close();
  }
}

// Run the example
main();

பயிற்சி

MongoDB இல், ஒரு தொகுப்பிலிருந்து தரவைத் தேர்ந்தெடுக்க, நாங்கள் ______ முறைகளைப் பயன்படுத்துகிறோம்.

SELECT மற்றும் SELECT ALL
✗ தவறு! SELECT மற்றும் SELECT ALL என்பது SQL இல் பயன்படுத்தப்படும் அறிக்கைகள், MongoDB முறைகள் அல்ல
GET மற்றும் GET ALL
✗ தவறு! GET மற்றும் GET ALL என்பது MongoDB இல் செல்லுபடியாகும் முறைகள் அல்ல
find மற்றும் findOne
✓ சரி! MongoDB இல், ஒரு தொகுப்பிலிருந்து தரவைத் தேர்ந்தெடுக்க find மற்றும் findOne முறைகளைப் பயன்படுத்துகிறோம். findOne ஒரு ஆவணத்தைத் திருப்பித் தரும், find பல ஆவணங்களைத் திருப்பித் தரும்
QUERY மற்றும் FETCH
✗ தவறு! QUERY மற்றும் FETCH என்பது MongoDB இல் செல்லுபடியாகும் முறைகள் அல்ல